package com.dataseek.iot.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dataseek.iot.sys.entity.NotifyParm;
import com.dataseek.iot.sys.entity.NotifyTemplate1;
import com.dataseek.iot.sys.mapper.NotifyParmMapper;
import com.dataseek.iot.sys.mapper.NotifyTemplateMapper1;
import com.dataseek.iot.sys.service.NotifyTemplateService;
import com.dataseek.iot.sys.vo.NotifyTemplateVo;
import com.dataseek.iot.toolkit.tenant.config.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service
@Slf4j
public class NotifyTemplateServiceImpl extends ServiceImpl<NotifyTemplateMapper1, NotifyTemplate1> implements NotifyTemplateService {


    @Autowired
    private NotifyTemplateMapper1 notifyTemplateMapper1;

    @Autowired
    private NotifyParmMapper notifyParmMapper;

    @Override
    public NotifyTemplateVo queryAction(String templateName, String templateCode, Integer num, Integer size) {

        NotifyTemplateVo templateVo = new NotifyTemplateVo();
        QueryWrapper<NotifyTemplate1> qw = new QueryWrapper<>();
        if (templateName != null && !templateName.isEmpty()) {
            qw.like("template_name", templateName);
        }
        if (templateCode != null && !templateCode.isEmpty()) {
            qw.like("template_code", templateCode);
        }
        IPage<NotifyTemplate1> page = new Page<>(num, size);
        List<NotifyTemplate1> mapList = notifyTemplateMapper1.selectPage(page, qw).getRecords();
        templateVo.setPage(num);
        templateVo.setSize(size);
        templateVo.setTotal(page.getTotal());
        templateVo.setList(mapList);
        return templateVo;
    }

    @Override
    public String insertAction(NotifyTemplate1 notifyTemplate1) {
        String tenantCode = DynamicDataSourceContextHolder.getDataSourceKey();
        QueryWrapper<NotifyTemplate1> qw = new QueryWrapper<>();
        qw.eq("template_name", notifyTemplate1.getTemplateName()).or().eq("template_code", notifyTemplate1.getTemplateCode());
        Integer count = notifyTemplateMapper1.selectCount(qw);
        if (count != 0) {
            return "999";
        }

        //模板正文中的参数匹配校验
        String errParam = this.checkParam(notifyTemplate1.getNotifyText());
        if (errParam != null) {
            return errParam;
        }

        if (notifyTemplate1.getCreateBy() == null) {
            if (tenantCode != null && !tenantCode.isEmpty()) {
                notifyTemplate1.setCreateBy(tenantCode);
                notifyTemplate1.setUpdateBy(tenantCode);
            }
            else {
                notifyTemplate1.setCreateBy("");
                notifyTemplate1.setUpdateBy("");
            }
        }
        this.saveOrUpdate(notifyTemplate1);
        return "";
    }

    private String  checkParam(String inputText) {
        String errParam = null;
        Pattern  patten = Pattern.compile("[$][\\{]([^$]*?)[\\}]");
        Matcher matcher = patten.matcher(inputText);
        while (matcher.find()) {
            String tmpParam = matcher.group(1);
            QueryWrapper<NotifyParm> qw = new QueryWrapper<>();
            qw.eq("parameter_code", tmpParam);
            NotifyParm findParam = notifyParmMapper.selectOne(qw);
            if (findParam == null) {
                errParam = tmpParam;
                break;
            }
        }
        return errParam;
    }



    @Override
    public String updateAction(NotifyTemplate1 notifyTemplate1) {

        //模板正文中的参数匹配校验
        String errParam = this.checkParam(notifyTemplate1.getNotifyText());
        if (errParam != null) {
            return errParam;
        }

        notifyTemplateMapper1.updateById(notifyTemplate1);
        return "";
    }

    @Override
    public Integer deleteAction(List<Integer> ids) {
        int resultCode = notifyTemplateMapper1.deleteBatchIds(ids);
        return resultCode;
    }

    public static void main(String[] args) {
        String dd ="1111${userName}ttytytytt${userCode}uuuuuuuuuuuu${weiGang}";
        Pattern  patten = Pattern.compile("[$][\\{]([^$]*?)[\\}]");
        Matcher matcher = patten.matcher(dd);
        while (matcher.find()) {
            System.out.print(matcher.group(1));
        }
    }

}
